package com.topscore.uke.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.topscore.uke.R;
import com.topscore.uke.entities.SelectorEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 71468 on 2017/10/25.
 */

public class BottomListSelector<T extends SelectorEntity> extends Dialog {

    @Bind(R.id.cancel_btn)
    RelativeLayout cancelBtn;
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.confirm_btn)
    RelativeLayout confirmBtn;
    @Bind(R.id.selector_recycler)
    RecyclerView selectorRecycler;

    private List<T> mListData;
    private String mTitle;

    private Context mContext;

    private SelectorCallBack mCallBack;

    private T mSelectedItem;

    private List<T> mSelectedItems;

    public BottomListSelector(@NonNull Context context, String title, List<T> listData) {
        super(context);
        this.mContext = context;
        this.mListData = listData;
        this.mTitle = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setWindowAnimations(R.style.DialogBottomAnimation);
        window.setGravity(Gravity.BOTTOM);
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.bottom_selector_layout);
        ButterKnife.bind(this);
        initViews();
    }

    private void initViews() {
        titleTv.setText(mTitle);
        selectorRecycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        selectorRecycler.setAdapter(new BaseQuickAdapter<T, BaseViewHolder>(R.layout.bottom_selector_item, mListData) {
            @Override
            public void onBindViewHolder(BaseViewHolder holder, int position, List<Object> payloads) {
//                super.onBindViewHolder(holder, position, payloads);
                if (payloads.isEmpty()){
                    onBindViewHolder(holder,position);
                }else {
                    T item = getData().get(position);
                    TextView item_title = holder.getView(R.id.item_title);
                    item_title.setText(item.getTitle());
                    ImageView check = holder.getView(R.id.check_iv);
                    if (item.isSelected()) {
                        check.setVisibility(View.VISIBLE);
                        item_title.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
                    } else {
                        check.setVisibility(View.GONE);
                        item_title.setTextColor(Color.parseColor("#131313"));
                    }
                }
            }

            @Override
            protected void convert(BaseViewHolder holder, T item) {
                TextView item_title = holder.getView(R.id.item_title);
                item_title.setText(item.getTitle());
                ImageView check = holder.getView(R.id.check_iv);
                if (item.isSelected()) {
                    check.setVisibility(View.VISIBLE);
                    item_title.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
                } else {
                    check.setVisibility(View.GONE);
                    item_title.setTextColor(Color.parseColor("#131313"));
                }
                holder.getConvertView().setOnClickListener(v -> {
                    if (mCallBack != null) {
                        if (mCallBack instanceof SelectorMultiCallBack) {
                            item.setSelected(!item.isSelected());
                            notifyItemChanged(holder.getLayoutPosition(), "check");
                            if (item.isSelected()) {
                                if (mSelectedItems == null) {
                                    mSelectedItems = new ArrayList<>();
                                }
                                mSelectedItems.add(item);
                            } else {
                                mSelectedItems.remove(item);
                            }
                        } else if (mCallBack instanceof SelectorSingleCallBack) {
                            if (!item.isSelected()) {
                                for (int i = 0; i < getData().size(); i++) {
                                    getData().get(i).setSelected(false);
                                }
                                item.setSelected(true);
                                mSelectedItem = item;
                                notifyDataSetChanged();
                            }
                        }
                    }
                });
            }
        });

        cancelBtn.setOnClickListener(v -> dismiss());
        confirmBtn.setOnClickListener(v -> {
            if (mCallBack == null) {
                Toast.makeText(mContext, "未设置回调接口", Toast.LENGTH_SHORT).show();
                return;
            }
            if (mCallBack instanceof SelectorSingleCallBack) {
                if (mSelectedItem == null) {
                    Toast.makeText(mContext, "请选择后确认", Toast.LENGTH_SHORT).show();
                    return;
                }
                ((SelectorSingleCallBack) mCallBack).onSelected(mSelectedItem);
                dismiss();
                return;
            }

            if (mCallBack instanceof SelectorMultiCallBack) {
                if (mSelectedItems == null || mSelectedItems.isEmpty()) {
                    Toast.makeText(mContext, "请选择后确认", Toast.LENGTH_SHORT).show();
                    return;
                }
                ((SelectorMultiCallBack) mCallBack).onSelected(mSelectedItems);
                dismiss();
                return;
            }
        });
    }

    @Override
    public void show() {
        super.show();
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
    }

    public BottomListSelector setCallBack(SelectorCallBack callBack) {
        mCallBack = callBack;
        return this;
    }

    private interface SelectorCallBack {

    }

    public interface SelectorSingleCallBack<T extends SelectorEntity> extends SelectorCallBack {
        void onSelected(T item);
    }

    public interface SelectorMultiCallBack<T extends SelectorEntity> extends SelectorCallBack {
        void onSelected(List<T> items);
    }
}
